package mine;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MineModel {
	private String[][] mine;
	private String[][] chess;

	public String[][] getMine() {
		return mine;
	}

	public void setMine(String[][] mine) {
		this.mine = mine;
	}

	public void setChess(String[][] chess) {
		this.chess = chess;
	}

	public MineModel(int i, int j) {
		chess = new String[i][i];
		this.mine = new String[i][i];
	}

	public void initModel(int i, int j) {
		for (int j2 = 0; j2 < mine.length; j2++) {
			for (int k = 0; k < mine[j2].length; k++) {
				mine[j2][k] = "0";
				chess[j2][k] = "+";
			}
		}
		for (int j2 = 0; j2 < j; j2++) {
			int x = new Random().nextInt(i);
			int y = new Random().nextInt(i);
			if (!mine[x][y].equals("@")) {
				mine[x][y] = "@";
				for (int k = 0; k < mine.length; k++) {
					for (int k2 = 0; k2 < mine[k].length; k2++) {
						if (mine[k][k2].equals("@")) {
							continue;
						}
						if ((x - k) * (x - k) + (y - k2) * (y - k2) == 1
								|| (x - k) * (x - k) + (y - k2) * (y - k2) == 2) {
							mine[k][k2] = Integer.toString(Integer
									.parseInt(mine[k][k2]) + 1);
						}
					}
				}
			} else {
				j2--;
			}
		}
	}

	public void decide(int i, int j) {
		if (chess[i][j].equals("*")) {
			return;
		}
		if (chess[i][j].equals("%")) {
			return;
		}
		if (!mine[i][j].equals("0") && !mine[i][j].equals("@")) {
			if (!chess[i][j].equals("*")) {
				chess[i][j] = mine[i][j];
			}
		}
		if (mine[i][j].equals("@")) {
			for (int j2 = 0; j2 < mine.length; j2++) {
				for (int k = 0; k < mine[i].length; k++) {
					if (mine[j2][k].equals("@") && !chess[j2][k].equals("*")) {
						chess[j2][k] = mine[i][j];
					}
					if (mine[j2][k].equals("@") && chess[j2][k].equals("*")) {
						chess[j2][k] = "$";
					}
					if (!mine[j2][k].equals("@") && chess[j2][k].equals("*")) {
						chess[j2][k] = "#";
					}
				}
			}
			chess[i][j] = "&";
		}
		if (mine[i][j].equals("0")) {
			List<Integer> list = new ArrayList<Integer>();
			List<Integer> l = new ArrayList<Integer>();
			chess[i][j] = mine[i][j];
			add(list, l, i, j);
			while (list.size() > 0) {
				int a = list.get(0) / 100;
				int b = list.get(0) % 100;
				if (!chess[a][b].equals("*")) {
					chess[a][b] = mine[a][b];
				}
				add(list, l, a, b);
				l.add(list.get(0));
				list.remove(0);
				if (list.size() == 0) {
					break;
				}
			}
		}
	}

	public void addf(int i, int j) {
		if (!chess[i][j].equals("*")
				&& !(chess[i][j].equals("+") || chess[i][j].equals("++"))
				&& !chess[i][j].equals("%")) {
			return;
		}
		if (chess[i][j].equals("*")) {
			chess[i][j] = "%";
			return;
		}
		if (chess[i][j].equals("%")) {
			chess[i][j] = "++";
			return;
		}
		chess[i][j] = "*";
	}

	// mine为数据表，chess为界面表

	public boolean isWin(int num) {
		for (int i = 0; i < mine.length; i++) {
			for (int j = 0; j < mine[i].length; j++) {
				if (mine[i][j].equals("@") && !chess[i][j].equals("*")
						|| num != 0) {
					return false;
				}
			}
		}
		for (int i = 0; i < mine.length; i++) {
			for (int j = 0; j < mine[i].length; j++) {
				if (chess[i][j].equals("+")) {
					if (mine[i][j].equals("@")) {
						chess[i][j] = "";
					} else {
						chess[i][j] = mine[i][j];
					}
				}
			}
		}
		return true;
	}

	public boolean isfail() {
		for (int i = 0; i < chess.length; i++) {
			for (int j = 0; j < chess[i].length; j++) {
				if (chess[i][j].equals("@")) {
					return true;
				}
			}
		}
		return false;
	}

	public void add(List<Integer> list, List<Integer> x, int i, int j) {
		for (int k2 = 0; k2 < mine.length; k2++) {
			for (int l = 0; l < mine[k2].length; l++) {
				if ((i - k2) * (i - k2) + (j - l) * (j - l) == 1
						|| (i - k2) * (i - k2) + (j - l) * (j - l) == 2) {
					if (mine[k2][l].equals("0")) {
						if (chess[k2][l].equals("%")
								|| chess[k2][l].equals("*")) {
							continue;
						}
						if (x.contains(k2 * 100 + l)
								|| list.contains(k2 * 100 + l)) {
							continue;
						}
						if (!chess[k2][l].equals("*")) {
							chess[k2][l] = mine[k2][l];
						}
						list.add(k2 * 100 + l);
					}
					if (!mine[k2][l].equals("0") && !chess[k2][l].equals("*")) {
						chess[k2][l] = mine[k2][l];
					}
				}
			}
		}
	}

	public String[][] getChess() {
		return this.chess;
	}

	public void addchess(int i, int j) {
		chess[i][j] = "*";
	}

	public boolean see(int x, int y) {
		String str = chess[x][y];
		if (!(str.equals("1") || str.equals("2") || str.equals("3")
				|| str.equals("4") || str.equals("5") || str.equals("6")
				|| str.equals("7") || str.equals("8"))) {
			return false;
		}
		int minenum = 0;
		for (int i = 0; i < mine.length; i++) {
			for (int j = 0; j < mine[i].length; j++) {
				if ((x - i) * (x - i) + (y - j) * (y - j) == 1
						|| (x - i) * (x - i) + (y - j) * (y - j) == 2) {
					if (chess[i][j].equals("*")) {
						minenum++;
					}
				}
			}

		}
		if (Integer.parseInt(chess[x][y]) == minenum) {
			return true;
		}
		return false;
	}
}
